<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :tree-node="companyInfo" @on-add="addOnClick" />
        <!-- 树状表 -->
        <!-- 树形组件的数据 -->
        <el-tree :data="treeData">
          <TreeTools slot-scope="scope" :tree-node="scope.data" @on-add="addOnClick" @on-companyList="loadDapartments" />

        </el-tree>

      </el-card>
    </div>
    <!-- 弹出层组件 -->
    <DeptsDialog :tree-node="node" :add-dialog-visible.sync="addDialogVisible" />
  </div>
</template>

<script>
import { getDapartmentList } from '@/api/dapartment'
import TreeTools from './components/tree-tools.vue'
import DeptsDialog from './components/depts-dialog.vue'
// 引入数据处理
import { listData } from '@/utils/index'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    DeptsDialog
  },
  data() {
    return {
      // 树形组件的数据
      treeData: [],
      companyInfo: {
        name: '',
        manager: '负责人',
        root: true // 显示查看，删除
      },
      addDialogVisible: false,
      node: {}

    }
  },

  created() {
    // 获取列表数据
    this.loadDapartments()
  },

  methods: {
    // 获取列表数据
    async loadDapartments() {
      const res = await getDapartmentList()
      console.log(res)
      // 动态公司名
      this.companyInfo.name = res.companyName
      // 筛选好的树状数组
      this.treeData = listData(res.depts, '')
      this.companyInfo.children = this.treeData
      // console.log(this.treeData)
    },
    addOnClick(node) {
      this.addDialogVisible = true
      this.node = node
    }
  }
}
</script>

<style scoped lang='scss'>
.tree-card {
  font-size:14px;
}
.el-row {
  width: 100%;
}
</style>
